package down.xiaoheihe.work.Service;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import down.xiaoheihe.work.utils.COSConfig;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.UUID;

@Service("TencentOssService")
public class TencentOssServiceImpl implements OssService {

    private static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    @Override
    public String uploadFileAvatar(MultipartFile file,String path)  {
        String URL = COSConfig.URL;
        String SECRET_ID = COSConfig.SECRET_ID;
        String SECRET_KEY = COSConfig.SECRET_KEY;
        String BUCKET_NAME = COSConfig.BUCKET_NAME;
        String PREFIX = COSConfig.PREFIX;
        String REGION = COSConfig.REGION;
        // 创建COS 凭证
        COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        // 配置 COS 区域 就购买时选择的区域 我这里是 广州（guangzhou）
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        //创建 COS 客户端连接
        COSClient cosClient = new COSClient(credentials, clientConfig);
        String fileName = file.getOriginalFilename();
        try {
            System.out.println(fileName);
            String substring = fileName.substring(fileName.lastIndexOf("."));
            File localFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), substring);
            file.transferTo(localFile);
            Random random = new Random();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = PREFIX + "/"+path+"/"+uuid + substring;
            // 将 文件上传至 COS
            PutObjectRequest objectRequest = new PutObjectRequest(BUCKET_NAME, fileName, localFile);
            cosClient.putObject(objectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return URL + fileName;
    }

    /**
     * 删除
     *
     * @param fileKey
     * @return
     */
    public String deleteBlog(String fileKey) {
        String URL = COSConfig.URL;
        String SECRET_ID = COSConfig.SECRET_ID;
        String SECRET_KEY = COSConfig.SECRET_KEY;
        String BUCKET_NAME = COSConfig.BUCKET_NAME;
        String PREFIX = COSConfig.PREFIX;
        String REGION = COSConfig.REGION;
        // 创建COS 凭证
        COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        // 配置 COS 区域 就购买时选择的区域 我这里是 广州（guangzhou）
        ClientConfig clientConfig = new ClientConfig(new Region(REGION));
        //创建 COS 客户端连接
        COSClient cosClient = new COSClient(credentials, clientConfig);
        //Bucket的命名格式为 BucketName-APPID ，此处填写的存储桶名称必须为此格式
        //指定被删除的文件在 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示删除位于 folder 路径下的文件 picture.jpg
        cosClient.deleteObject(BUCKET_NAME, fileKey);
        return "ok";
    }
}
